<template>

  <div class="password-container">

    <div class="password-logo">
      <img src="@/logo.jpeg" alt="">
      <span>龙山双创基地数据系统 </span>
    </div>

    <el-row class="password-item" style="display: flex;justify-content: center;">
      <el-col :span="10" class="password-left">
        <div >
          <img class="password-banner" src="./images/password-banner.png" alt="">
        </div>
      </el-col>
      <el-col :span="10" class="password-right">
        <el-form ref="passwordForm" :model="passwordForm" :rules="passwordRules" class="password-form" autocomplete="off" label-position="left">
          <div class="title-container">
            <h3 class="title">
              重置密码
            </h3>
          </div>

          <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
            <el-input
              ref="username"
              v-model="passwordForm.username"
              :placeholder="$t('password.username')"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="off"
            />
          </el-form-item>

          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-input type="text" tabindex="2" ref="verifyCode" placeholder="请输入验证码" class="verifyCode" v-model="passwordForm.verifyCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="11" :offset="1">
              <el-button :loading="loading" type="primary" style="width:100%;height: 47px;" @click.native.prevent="sendMsg">
                {{ verifyCodeTip }}
              </el-button>
            </el-col>
          </el-row>

          <el-tooltip v-model="capsTooltip" content="大写字母" placement="right" manual>
            <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
              <el-input
                :key="passwordType"
                ref="password"
                v-model="passwordForm.password"
                :type="passwordType"
                :placeholder="$t('password.password')"
                name="password"
                tabindex="3"
                autocomplete="off"
                @keyup.native="checkCapslock"
                @blur="capsTooltip = false"
                @keyup.enter.native="handleLogin"
              />
              <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
            </el-form-item>
          </el-tooltip>

          <el-button :loading="loading" type="primary" style="width:100%;margin-top: 30px;" @click.native.prevent="handlePassword">
            重置密码
          </el-button>


        </el-form>
      </el-col>
    </el-row>

    <div class="password-bottom">
      <!--<p>用技术连接和重构实体产业生态圈</p> -->
      <p>Copyright  ©20 20 厦门市龙山文化创意产业有限公司 All Rights Reserved. 闽ICP备13006726号</p>
    </div>

  </div>
</template>

<script>
    import { validUsername } from '@/utils/validate'
    import utils from '@/utils/utils.js'
    import ElRow from "element-ui/packages/row/src/row";
    import ElCol from "element-ui/packages/col/src/col";
    import ElCheckbox from "../../../node_modules/element-ui/packages/checkbox/src/checkbox";
    import { sendMsg } from '@/api/sysMsg'
    import { resetPwd } from '@/api/user'
    import md5 from 'js-md5';

    export default {
        components: {
            ElCheckbox,
            ElCol,
            ElRow},
        name: 'password',
        data() {
            const validateUsername = (rule, value, callback) => {
                console.log(value);
                if (validUsername(value)) {
                    callback(new Error('请输入正确的手机号/邮箱'))
                } else {
                    callback()
                }
            }
            const validatePassword = (rule, value, callback) => {
                if (value.length < 6) {
                    callback(new Error('密码长度不能小于6位'))
                } else {
                    callback()
                }
            }
            return {
                passwordForm: {
                    username: '',
                    password: '',
                    verifyCode: '',
                    verifyCodeKey: ''
                },
                checked:false,
                passwordRules: {
                    username: [{ required: true, trigger: 'blur', validator: validateUsername }],
                    password: [{ required: true, trigger: 'blur', validator: validatePassword }],
                    verifyCode: [{ required: true, trigger: 'blur' }]
                },
                passwordType: 'password',
                capsTooltip: false,
                loading: false,
                redirect: undefined,
                verifyCodeTip: '发送验证码',
                counter:0,
                counterTimer:null
            }
        },
        mounted() {
            if (this.passwordForm.username === '') {
                this.$refs.username.focus()
            } else if (this.passwordForm.password === '') {
                this.$refs.password.focus()
            } else if (this.passwordForm.verifyCode === '') {
                this.$refs.verifyCode.focus()
            }
        },
        watch: {
            $route: {
                handler: function(route) {
                    const query = route.query
                    if (query) {
                        this.redirect = query.redirect
                    }
                },
                immediate: true
            }
        },
        created() {
            var _this = this;
            this.counterTimer = setInterval(function () {
                if (_this.counter>0) {
                    _this.verifyCodeTip = "剩余时间"+_this.counter+"秒";
                    _this.counter = _this.counter -1;
                } else {
                    _this.verifyCodeTip = "发送验证码";
                }
            },1000)
            this.passwordForm.verifyCodeKey = utils.getUUID()
            // window.addEventListener('storage', this.afterQRScan)
        },
        destroyed() {
            clearInterval(this.counterTimer);

        },
        methods: {
            // 发送验证码
            sendMsg() {
                if (this.counter>0) {
                    return;
                }
                var _this = this;
                _this.loading = true;
                sendMsg({'username':this.passwordForm.username,tplKey:'register'}).then(response => {
                    console.log(response)
                    _this.loading = false
                    _this.counter = 60;
                }).catch(error => {
                    _this.loading = false
                })

            },
            checkCapslock(e) {
                const { key } = e
                this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
            },
            showPwd() {
                if (this.passwordType === 'password') {
                    this.passwordType = ''
                } else {
                    this.passwordType = 'password'
                }
                this.$nextTick(() => {
                    this.$refs.password.focus()
                })
            },
            handlePassword() {
                var _this = this;
                this.$refs.passwordForm.validate(valid => {
                    if (valid) {
                        this.loading = true
                        var _this = this;
                        let params = {
                            username: _this.passwordForm.username,
                            password: md5(_this.passwordForm.password),
                            verifyCode: _this.passwordForm.verifyCode
                        }
                        resetPwd(params).then(response => {
                            _this.loading = false
                            _this.$message("重置密码成功,请登录")
                            _this.$router.push({
                                path: '/login'
                            })
                        }).catch(error => {
                            _this.loading = false
                        })
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            }
        }
    }
</script>

<style lang="scss">

  /* 修复input 背景不协调 和光标变色 */
  /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

  //$bg:#283443;
  $bg:#fff;
  //$light_gray:#fff;
  $light_gray: 2d3a4b;
  //$cursor: #fff;
  $cursor: #2d3a4b;

  @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
    .password-container .el-input input {
      color: $cursor;
    }
  }

  /* reset element-ui style */
  .password-container {
    position: relative;
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;
        caret-color: $cursor;

        &:-webkit-autofill {
          box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: $cursor !important;
        }
      }
    }

    .el-form-item {
      /*border: 1px solid rgba(255, 255, 255, 0.1);*/
      /*background: rgba(0, 0, 0, 0.1);*/
      /*border-radius: 5px;*/
      /*color: #454545;*/
    }

  }
</style>

<style lang="scss" scoped>
  //$bg:#2d3a4b;
  $bg: #fafafa;
  $dark_gray:#889aa4;
  //$light_gray:#eee;
  $light_gray:#2d3a4b;

  .password-container {
    min-height: 100%;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .password-logo{
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 405px;
      height: 100px;
      padding: 50px 40px 10px 40px;
      img{
        width: 60px;
        height: 60px;
      }
      span{
        font-size: 30px;
        font-weight: bold;
      }
    }

    .password-left{
      width: 400px;
      height: 400px;
      .password-banner{
        width: 100%;
      }
    }

    .password-right{
      width: 400px;
      height: 400px;
      padding:0 50px;
      position: relative;
      background: #fff;
    }

    .password-form {
      position: relative;
      width: 520px;
      max-width: 100%;
      padding:  35px 0;
      margin: 0 auto;
      overflow: hidden;
      .el-form-item {
        border: 1px solid rgba(0,0,0,0.1);
        border-radius: 5px;
        color: #454545;
      }
    }

    .tips {
      margin-bottom: 20px;
      font-size: 14px;
      color: #fff;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title-container {
      position: relative;

      .title {
        font-size: 20px;
        color: $light_gray;
        margin: 0px auto 20px auto;
        text-align: center;
        font-weight: bold;
      }

      .set-language {
        color: #fff;
        position: absolute;
        top: 3px;
        font-size: 18px;
        right: 0px;
        cursor: pointer;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .sendMsg-button {
      position: absolute;
      right: 0;
      bottom: 0px;
    }


    .password-bottom{
      line-height: 30px;
      text-align: center;
      margin-bottom: 30px;
      p:nth-child(1){
        font-size: 17px;
        color: $light_gray;
      }
      p:nth-child(2){
        font-size: 10px;
        color: $light_gray;
      }
    }

    @media only screen and (max-width: 470px) {
      .thirdparty-button {
        display: none;
      }
    }
  }

</style>
